package com.bcu.alumnus.service;

import com.bcu.alumnus.entity.Apply;
import com.bcu.alumnus.entity.Message;
import com.bcu.alumnus.repo.ActivityRepository;
import com.bcu.alumnus.repo.ApplyRepository;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Validated
@Transactional
public class ApplyService {

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Resource
    private ApplyRepository applyRepository;
    @Resource
    private ActivityRepository activityRepository;

    /**
     * @Author: GuoZiZhou
     * @Date: 15:55 2020/4/21
     * @Description:添加报名信息
     */
    public Message<Apply> insertApply(Apply apply){
        logger.info("添加活动申请，活动编号{}，报名者姓名{}",apply.getApplyId(),apply.getApplyUserName());
        if(activityRepository.getActMemberWay((Integer.parseInt(apply.getApplyActivityId()))).equals("1")){
            apply.setApplyStatus("0");
        }else{
            apply.setApplyStatus("1");
        }
        apply.setApplyCreateTime(new Date());
        Apply rs = applyRepository.save(apply);
        logger.info("校园新闻已添加，编号：{}",rs.getApplyId());
        return Message.success(null).add(rs);
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 10:29 2020/4/20
     * @Description: 根据活动编号查询已录取的报名信息
     */
    public Message<List<Apply>> getApplyByApplyActivityIdAndApplyStatus(String ApplyActivityId){
        logger.info("根据活动编号查询已录取的报名信息，活动编号：{}",ApplyActivityId);
        return Message.success(null).add(applyRepository.getApplyByApplyActivityIdAndApplyStatus(ApplyActivityId,"1"));
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 10:41 2020/4/20
     * @Description: 根据活动编号查询所有的报名信息
     */
    /**
     * 错误！ 方法名应该为：getApplyByActivityId ，却被写为 getApplyByApplyId
     * 王镭树 4.28 23：58
     */
    public Message<List<Apply>> getApplyByActivityId(String activityId){
        logger.info("根据活动编号查询所有的报名信息，活动编号：{}",activityId);
        return Message.success(null).add(applyRepository.getApplyByApplyActivityId(activityId));
    }

    /**
    * @Author: Wls
    * @Date: 0:03 2020/4/29
    * @Description: 【补充】 根据报名申请编号获取报名详细信息
    */
    public Message<Apply> getApplyByApplyId(Integer applyId){
        logger.info("根据活动编号查询所有的报名信息，报名编号：{}",applyId);
        return Message.success(null).add(applyRepository.getApplyByApplyId(applyId));
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 10:47 2020/4/20
     * @Description: 根据用户编号查询所有的报名信息
     */
    public Message<List<Apply>> getApplyByApplyUserId(String ApplyUserId){
        logger.info("根据用户编号查询所有的报名信息，用户编号：{}",ApplyUserId);
        return Message.success(null).add(applyRepository.getApplyByApplyUserId(ApplyUserId));
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 10:56 2020/4/20
     * @Description: 通过用户编号查询和录取状态查询报名信息
     */
    public Message<List<Apply>> getApplyByApplyUserIdAndApplyStatus(String ApplyUserId,String ApplyStatus){
        logger.info("根据用户编号查询所有的报名信息，用户编号：{},录取状态：{}",ApplyUserId,ApplyStatus);
        return Message.success(null).add(applyRepository.getApplyByApplyUserIdAndApplyStatus(ApplyUserId,ApplyStatus));
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 11:18 2020/4/20
     * @Description: 通过活动编号统计报名数量
     */
    public Message countApplyByApplyActivityId(String ApplyActivityId){
        logger.info("通过活动编号统计报名数量，活动编号：{}",ApplyActivityId);
        return Message.success(null).add(applyRepository.countApplyByApplyActivityId(ApplyActivityId));
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 11:37 2020/4/20
     * @Description: 通过活动编号统计已录取的报名数量
     */
    public Message countApplyByApplyActivityIdAndApplyStatus(String ApplyActivityId){
        logger.info("通过活动编号统计已录取的报名数量，活动编号：{}",ApplyActivityId);
        return Message.success(null).add(applyRepository.countApplyByApplyActivityIdAndApplyStatus(ApplyActivityId,"1"));
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 15:24 2020/4/20
     * @Description: 通过活动编号、用户编号修改录取状态
     */
    public Message UpdateApplyStatusByApplyActivityIdAndApplyUserId(String ApplyActivityId,String ApplyUserId){
        logger.info("通过活动编号、用户编号修改录取状态,活动编号：{}，用户编号：{}",ApplyActivityId,ApplyUserId);
        int result = applyRepository.updateApplyStatusByApplyActivityIdAndApplyUserId(ApplyActivityId,ApplyUserId);
        if(result>0){
            return Message.success(null).add(result);
        }else{
            return Message.fail(null).add(result);
        }
    }

    /**
     * @Author: GuoZiZhou
     * @Date: 15:24 2020/4/20
     * @Description: 根据活动编号将已录取的报名记录导出为Excel
     */
    public Message OutExcel(String ActivityId) throws IOException {
        logger.info("根据活动编号将已录取的报名记录导出为Excel,活动编号：{}",ActivityId);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("活动Id"+ActivityId+" "+new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date()).toString());

        HSSFRow row1 = sheet.createRow(0);
        row1.createCell(0).setCellValue("applyId");
        row1.createCell(1).setCellValue("applyActivityId");
        row1.createCell(2).setCellValue("applyUserId");
        row1.createCell(3).setCellValue("applyUserName");
        row1.createCell(4).setCellValue("applyUserPart");
        row1.createCell(5).setCellValue("applyUserClass");
        row1.createCell(6).setCellValue("applyUserPhone");
        row1.createCell(7).setCellValue("applyUserEmail");
        row1.createCell(8).setCellValue("applyCreateTime");
        row1.createCell(9).setCellValue("applyStatus");
        row1.createCell(10).setCellValue("applyRemark");

        List<Apply> applies = applyRepository.getApplyByApplyActivityIdAndApplyStatus(ActivityId,"1");
        for(int i=0;i<applies.size();i++){
            HSSFRow row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(applies.get(i).getApplyId().toString());
            row.createCell(1).setCellValue(applies.get(i).getApplyActivityId());
            row.createCell(2).setCellValue(applies.get(i).getApplyUserId());
            row.createCell(3).setCellValue(applies.get(i).getApplyUserName());
            row.createCell(4).setCellValue(applies.get(i).getApplyUserPart());
            row.createCell(5).setCellValue(applies.get(i).getApplyUserClass());
            row.createCell(6).setCellValue(applies.get(i).getApplyUserPhone());
            row.createCell(7).setCellValue(applies.get(i).getApplyUserEmail());
            row.createCell(8).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(applies.get(i).getApplyCreateTime()));
            row.createCell(9).setCellValue(applies.get(i).getApplyStatus());
            row.createCell(10).setCellValue(applies.get(i).getApplyRemark());
        }

        String path = "E:/"+"活动Id"+ActivityId+" "+new SimpleDateFormat("yyyy-MM-dd HH-mm").format(new Date()).toString()+".xls";
        File file = new File(path);
        FileOutputStream out = new FileOutputStream(path);
        wb.write(out);
        out.close();
        return Message.success(null);
    }
}